﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Eis51.CampanyCMS.Model.BaseInfoMng.QueryO;
using Eis51.CampanyCMS.Business.BaseInfoMng;
using Eis51.CampanyCMS.FrameWork;
using System.Data;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.WebUI.Admin.TemplatesMgnt
{
    public partial class list : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnSearch_Click(null, null);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BaseTemplatesQuery query = new BaseTemplatesQuery();
            query.TempName = txtTemplateName.Text;

            this.dgNews.Data = BaseTemplatesBLL.GetInstance().SearchByCondition(query);
            this.dgNews.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string url = "edit.aspx";

            url = UrlUtility.AddOrReplaceParam(url, "r", ReturnURL(), true);
            Response.Redirect(url);
        }

        protected void dgNews_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":

                    this.btnSearch_Click(null, null);
                    break;
                case "Modify":
                    string url = "edit.aspx?templateid={0}";
                    url = string.Format(url, e.Item.Cells[0].Text);
                    url = UrlUtility.AddOrReplaceParam(url, "r", ReturnURL(), true);
                    Response.Redirect(url);
                    break;
                default:
                    break;
            }
        }

        private string ReturnURL()
        {
            return Request.RawUrl;
        }
        protected void dgNews_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {


            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                LinkButton btnDelete = e.Item.FindControl("btnDelete") as LinkButton;
                LinkButton btnModify = e.Item.FindControl("btnModify") as LinkButton;
                Image img = e.Item.FindControl("img") as Image;
                DataRowView dv = e.Item.DataItem as DataRowView;
                img.ImageUrl = dv["Template_Thumbnail_path"].ToString();


            }
        }

    }
}
